import { readUriToTextAndExplode } from "../readFileToText/openTextFile";
import { RenderData } from "../classes/RadarRenderData";
import { SymbolConfig } from "../classes/SymbolConfig";
import { Coordinate } from "../coordParser/Coordinate";
import { DrawCommand, SymbolDrawDefinition } from "../classes/SymbolDrawer";

const addSymbolDrawer = (id: string, drawFunc: SymbolDrawDefinition) => {
    if (id == '0') {
        RenderData.airportSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '1') {
        RenderData.ndbSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '2') {
        RenderData.vorSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '3') {
        RenderData.fixSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '4') {
        RenderData.aStandbySymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '5') {
        RenderData.aPrimaryRadarOnlySymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '6') {
        RenderData.aCorrACModeSecRadarOnlySymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '7') {
        RenderData.aCorrSModeSecRadarOnlySymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '8') {
        RenderData.aCorrPrimAndACModeSecSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '9') {
        RenderData.aCorrPrimAndSModeSecSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '10') {
        RenderData.aCorrACModeIndentingSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '11') {
        RenderData.aCorrSModeIndentingSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '12') {
        RenderData.aFlightPlanTrackSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '13') {
        RenderData.aCoastingSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '14') {
        RenderData.historyDotSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '15') {
        RenderData.groundAircraftSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '16') {
        RenderData.aUnCorrACModeSecRadarOnlySymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '17') {
        RenderData.aUnCorrSModeSecRadarOnlySymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '18') {
        RenderData.aUnCorrPrimAndACModeSecSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '19') {
        RenderData.aUnCorrPrimAndSModeSecSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '20') {
        RenderData.aUnCorrACModeIndentingSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '21') {
        RenderData.aUnCorrSModeIndentingSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '22') {
        RenderData.groundVehicleSymbolDrawer.drawCommand.push(drawFunc);
    } else if (id == '23') {
        RenderData.groundRotorcraftSymbolDrawer.drawCommand.push(drawFunc);
    }
}
const resetSymbolDrawer = (id: string) => {
    if (id == '0') {
        RenderData.airportSymbolDrawer.reset();
    } else if (id == '1') {
        RenderData.ndbSymbolDrawer.reset();
    } else if (id == '2') {
        RenderData.vorSymbolDrawer.reset();
    } else if (id == '3') {
        RenderData.fixSymbolDrawer.reset();
    } else if (id == '4') {
        RenderData.aStandbySymbolDrawer.reset();
    } else if (id == '5') {
        RenderData.aPrimaryRadarOnlySymbolDrawer.reset();
    } else if (id == '6') {
        RenderData.aCorrACModeSecRadarOnlySymbolDrawer.reset();
    } else if (id == '7') {
        RenderData.aCorrSModeSecRadarOnlySymbolDrawer.reset();
    } else if (id == '8') {
        RenderData.aCorrPrimAndACModeSecSymbolDrawer.reset();
    } else if (id == '9') {
        RenderData.aCorrPrimAndSModeSecSymbolDrawer.reset();
    } else if (id == '10') {
        RenderData.aCorrACModeIndentingSymbolDrawer.reset();
    } else if (id == '11') {
        RenderData.aCorrSModeIndentingSymbolDrawer.reset();
    } else if (id == '12') {
        RenderData.aFlightPlanTrackSymbolDrawer.reset();
    } else if (id == '13') {
        RenderData.aCoastingSymbolDrawer.reset();
    } else if (id == '14') {
        RenderData.historyDotSymbolDrawer.reset();
    } else if (id == '15') {
        RenderData.groundAircraftSymbolDrawer.reset();
    } else if (id == '16') {
        RenderData.aUnCorrACModeSecRadarOnlySymbolDrawer.reset();
    } else if (id == '17') {
        RenderData.aUnCorrSModeSecRadarOnlySymbolDrawer.reset();
    } else if (id == '18') {
        RenderData.aUnCorrPrimAndACModeSecSymbolDrawer.reset();
    } else if (id == '19') {
        RenderData.aUnCorrPrimAndSModeSecSymbolDrawer.reset();
    } else if (id == '20') {
        RenderData.aUnCorrACModeIndentingSymbolDrawer.reset();
    } else if (id == '21') {
        RenderData.aUnCorrSModeIndentingSymbolDrawer.reset();
    } else if (id == '22') {
        RenderData.groundVehicleSymbolDrawer.reset();
    } else if (id == '23') {
        RenderData.groundRotorcraftSymbolDrawer.reset();
    }
}

export async function loadSymbologyFile(uri: string) {
    const lines = await readUriToTextAndExplode(uri);
    const symbologyRegex = new RegExp('(?<group>[^:]*):(?<type>[^:]*):(?<color>[^:]*):(?<size>[^:]*):(?<lineWeight>[^:]*):(?<lineStyle>[^:]*):(?<alignment>[^:]*)');
    const MOVETORegex = new RegExp('SYMBOLITEM:MOVETO (?<x>-?\\d+) (?<y>-?\\d+)');
    const LINETORegex = new RegExp('SYMBOLITEM:LINETO (?<x>-?\\d+) (?<y>-?\\d+)');
    const ARCRegex = new RegExp('SYMBOLITEM:ARC (?<x>-?\\d+) (?<y>-?\\d+) (?<radius>-?\\d+) (?<start>-?\\d+) (?<end>-?\\d+)');
    const FILLARCRegex = new RegExp('SYMBOLITEM:FILLARC (?<x>-?\\d+) (?<y>-?\\d+) (?<radius>-?\\d+) (?<start>-?\\d+) (?<end>-?\\d+)');
    const SETPXIELRegex = new RegExp('SYMBOLITEM:SETPIXEL (?<x>-?\\d+) (?<y>-?\\d+)');
    const POLYGONRegex = new RegExp('SYMBOLITEM:POLYGON .+');
    const SYMBOLRegex = new RegExp('SYMBOL:(?<id>\\d+)');
    let activeSymbolDrawerRefer: string = '';
    for (const line of lines) {
        if (symbologyRegex.test(line)) {
            const match = symbologyRegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                const symbol = new SymbolConfig();
                symbol.group = g.group;
                symbol.type = g.type;
                symbol.color = g.color;
                symbol.size = Number(g.size);
                symbol.lineWeight = Number(g.lineWeight);
                symbol.lineStyle = g.lineStyle;
                symbol.alignment = g.alignment;
                RenderData.symbols.set(`${symbol.group}-${symbol.type}`, symbol);
            }
        } else if (SYMBOLRegex.test(line)) {
            const match = SYMBOLRegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                activeSymbolDrawerRefer = g.id;
                resetSymbolDrawer(activeSymbolDrawerRefer);
            }
        } else if (MOVETORegex.test(line)) {
            const match = MOVETORegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                const x = Number(g.x);
                const y = Number(g.y);
                const command = new SymbolDrawDefinition();
                command.command = DrawCommand.MOVETO;
                const coord = new Coordinate();
                coord.longitude = x;
                coord.latitude = y;
                command.coords.push(coord);
                addSymbolDrawer(activeSymbolDrawerRefer, command);
            }
        } else if (LINETORegex.test(line)) {
            const match = LINETORegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                const x = Number(g.x);
                const y = Number(g.y);
                const command = new SymbolDrawDefinition();
                command.command = DrawCommand.LINETO;
                const coord = new Coordinate();
                coord.longitude = x;
                coord.latitude = y;
                command.coords.push(coord);
                addSymbolDrawer(activeSymbolDrawerRefer, command);
            }
        } else if (ARCRegex.test(line)) {
            const match = ARCRegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                const command = new SymbolDrawDefinition();
                command.command = DrawCommand.ARC;
                const coord = new Coordinate();
                coord.longitude = Number(g.x);
                coord.latitude = Number(g.y);
                command.coords.push(coord);
                command.radius = Number(g.radius);
                command.start = Number(g.start);
                command.end = Number(g.end);
                addSymbolDrawer(activeSymbolDrawerRefer, command);
            }
        } else if (FILLARCRegex.test(line)) {
            const match = FILLARCRegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                const command = new SymbolDrawDefinition();
                command.command = DrawCommand.FILL_ARC;
                const coord = new Coordinate();
                coord.longitude = Number(g.x);
                coord.latitude = Number(g.y);
                command.coords.push(coord);
                command.radius = Number(g.radius);
                command.start = Number(g.start);
                command.end = Number(g.end);
                addSymbolDrawer(activeSymbolDrawerRefer, command);
            }
        } else if (SETPXIELRegex.test(line)) {
            const match = SETPXIELRegex.exec(line);
            if (match != null && match.groups != undefined) {
                const g = match.groups;
                const command = new SymbolDrawDefinition();
                command.command = DrawCommand.PIXEL;
                const coord = new Coordinate();
                coord.longitude = Number(g.x);
                coord.latitude = Number(g.y);
                command.coords.push(coord);
                addSymbolDrawer(activeSymbolDrawerRefer, command);
            }
        } else if (POLYGONRegex.test(line)) {
            const poses = line.replace('SYMBOLITEM:POLYGON ', '').split(' ');
            if (poses.length % 2 != 0) continue;
            const command = new SymbolDrawDefinition();
            command.command = DrawCommand.POLYGON;
            for (let i = 0; i < poses.length; i += 2) {
                const x = Number(poses[i]);
                const y = Number(poses[i + 1]);
                const coord = new Coordinate();
                coord.longitude = x;
                coord.latitude = y;
                command.coords.push(coord);
            }
            addSymbolDrawer(activeSymbolDrawerRefer, command);
        }
    }
}